//
//  RHRunDataCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/1.
//

#import "RHRunDataCell.h"
@interface RHRunDataCell()


@end
@implementation RHRunDataCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    
    QMUILabel *lab1 = [self.contentView viewWithTag:200];
    lab1.text = [NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber  decimalStringWithDouble:[dic[@"todayElectricity"]doubleValue]]];
    lab1.attributedText = [self attrStringWithContent:lab1.text attrKey:@"(kWh)"];
    
    QMUILabel *lab2 = [self.contentView viewWithTag:201];
    lab2.text = [NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber  decimalStringWithDouble:[dic[@"todayGeneration"]doubleValue]]];
    lab2.attributedText = [self attrStringWithContent:lab2.text attrKey:@"(kWh)"];
     
    QMUILabel *lab3 = [self.contentView viewWithTag:202];
    lab3.text = [NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber  decimalStringWithDouble:[dic[@"todayChg"]doubleValue]]];
    lab3.attributedText = [self attrStringWithContent:lab3.text attrKey:@"(kWh)"];
    
    QMUILabel *lab4 = [self.contentView viewWithTag:203];
    lab4.text = [NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber  decimalStringWithDouble:[dic[@"todayDchg"]doubleValue]]];
    lab4.attributedText = [self attrStringWithContent:lab4.text attrKey:@"(kWh)"];
    
    QMUILabel *lab5 = [self.contentView viewWithTag:204];
    lab5.text = [NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber  decimalStringWithDouble:[dic[@"todayBuy"]doubleValue]]];
    lab5.attributedText = [self attrStringWithContent:lab5.text attrKey:@"(kWh)"];
    
    QMUILabel *lab6 = [self.contentView viewWithTag:205];
    lab6.text = [NSString stringWithFormat:@"%@(kWh)",[NSDecimalNumber  decimalStringWithDouble:[dic[@"todaySell"]doubleValue]]];
    lab6.attributedText = [self attrStringWithContent:lab6.text attrKey:@"(kWh)"];
    
}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    
    NSParagraphStyle *style = [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:22 lineBreakMode: NSLineBreakByTruncatingTail textAlignment: NSTextAlignmentCenter];
    
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6],NSParagraphStyleAttributeName:style};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}

-(void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    kWeakSelf;
    __block UIView *lastView;
    __block CGFloat itemW = (kUI_WINDOW_W-30)/2;
    NSArray *titles = @[@"负载用电".rh_localized,@"光伏发电".rh_localized,@"电池充电".rh_localized,@"电池放电".rh_localized,@"电网买电".rh_localized,@"电网卖电".rh_localized];
    NSArray *images = @[@"home_load_use",@"home_solar_generation",@"home_battery_chg",@"home_battery_dchg",@"home_grid_buy",@"home_grid_sell"];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [weakSelf itemViewWithIndex:idx title:titles[idx] image:images[idx]];
        [bgView addSubview:itemView];
        if(idx == 0){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(0);
                make.width.offset(itemW);
                make.height.offset(kScaleW(80));
            }];
            //
        }else{
            if(idx%2==0){//换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(0);
                    make.top.equalTo(lastView.mas_bottom);
                    make.size.equalTo(lastView);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
                
        lastView = itemView;
        
        //不是最后一行就加上一竖线
        if(idx % 2 == 0){
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"E7E7E7");
            [self addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(lastView).multipliedBy(0.5);
                make.width.offset(1);
                make.centerY.equalTo(lastView);
                make.right.equalTo(lastView);
            }];
        }
        if(idx % 2 == 0){
            //加横线
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"E7E7E7");
            [bgView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(35);
                make.right.mas_offset(-35);
                make.height.offset(1);
                make.top.equalTo(itemView.mas_bottom);
            }];
        }
        

    }];
    //封底
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    
}


-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr
{
    UIView *bgView = kView;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.left.mas_offset(20);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 0;
    titleLab.text = [NSString stringWithFormat:@"%@",title];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    titleLab.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = kSYSTEMFONT(14);
    [bgView addSubview:contentLab];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(8);
        make.right.mas_offset(-3);
        make.centerY.mas_offset(15);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentLab);
        make.centerY.mas_offset(-15);
    }];
    return bgView;
}

@end
